//
//  ZLQPictureCollecitionViewController.swift
//  GoTravel
//
//  Created by lanou3g on 16/3/5.
//  Copyright © 2016年 SHH. All rights reserved.
//

import UIKit

class ZLQPictureCollecitionViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

    var count:Int = 0
    var number:Int = 0
    
    //屏幕的宽和高
    let width = UIScreen.mainScreen().bounds.width
    let height = UIScreen.mainScreen().bounds.height
    
    var flowLayout:UICollectionViewFlowLayout?
    var collectionview:UICollectionView?
    override func viewDidLoad() {
        super.viewDidLoad()
        
        flowLayout = UICollectionViewFlowLayout()
        flowLayout?.sectionInset = UIEdgeInsetsMake(5, self.view.frame.size.height*0.005, self.view.frame.size.width*0.005, 0)
        flowLayout?.itemSize = CGSize(width: self.view.frame.size.width*0.18, height: self.view.frame.size.height*0.15)
        flowLayout?.minimumLineSpacing = 10
        
        collectionview = UICollectionView(frame: CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height*0.18), collectionViewLayout: flowLayout!)
        collectionview?.backgroundColor = UIColor.whiteColor()
        
        collectionview?.bounces = false
        collectionview?.delegate = self
        collectionview?.dataSource = self
        
        collectionview?.registerClass(ZLQPictureCollecitionCell.classForCoder(), forCellWithReuseIdentifier: "cellID")
        self.view.addSubview(collectionview!)

//        if count == 4 {
//        number = 2
//        }else if count == 8 {
//        number = 3
//        }
       }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
        

            
            let picker:UIImagePickerController = UIImagePickerController()
            picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
            picker.delegate = self
            
            self.presentViewController(picker, animated: true, completion: nil)
        }
        
//        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
//          let picker1 = UIImagePickerController()
//            picker1.sourceType = .Camera
//            picker1.editing = true
//            
//            self.presentViewController(picker1, animated: true, completion: nil)
//        }else {
//        
//            print("你没有摄像头")
//        }
        
        
    }
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        
        
       
        let a:NSIndexPath = (collectionview?.indexPathsForSelectedItems()?.first)!
        let cell = collectionview?.cellForItemAtIndexPath(a) as? ZLQPictureCollecitionCell
        cell?.picture?.image = info[UIImagePickerControllerOriginalImage] as? UIImage
        picker.dismissViewControllerAnimated(true, completion: nil)
        count+=1
        
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
                 self.collectionview?.reloadData()
        }
   
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
 
    /*
    // MARK: - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    }
    */
    
}

extension ZLQPictureCollecitionViewController:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return count+1

    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionview?.dequeueReusableCellWithReuseIdentifier("cellID", forIndexPath: indexPath) as! ZLQPictureCollecitionCell
       
        number = count
        if indexPath.row == number{
                cell.picture?.image = UIImage(named: "屏幕快照 2016-03-07 20.48.44")
            
        }

        
        return cell
    }
    
    
//    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
//        return CGSize(width: width/2, height: height/2)
//    }
//    
    
}
